package student.action;

import java.util.ArrayList;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.MyLog;

import edu.student.models.Enrollment;
import edu.student.models.Lookup;
import edu.student.models.Semester;
import edu.student.models.Subject;
import edu.student.models.User;

public class StudentEnrollment extends ActionSupport{
	private ArrayList<String> semesterList = new ArrayList<String>();
	private ArrayList<Lookup> allSemValues = new ArrayList<Lookup>();
	private ArrayList<Lookup> allCourseValues = new ArrayList<Lookup>();
	private ArrayList<String> subjectList  =new ArrayList<String>();
	private ArrayList<String> enrolledList  =new ArrayList<String>();
	private String commandButton = "";
	private String semester;
	private ArrayList<String> subject=new ArrayList<String>();
	private String msg;
	private String roll_no;
	public ArrayList<String> getSemesterList() {
		return semesterList;
	}

	public void setSemesterList(ArrayList<String> semesterList) {
		this.semesterList = semesterList;
	}
	
	public ArrayList<String> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(ArrayList<String> subjectList) {
		this.subjectList = subjectList;
	}
	
	public String getCommandButton() {
		return commandButton;
	}

	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
	
	public String getSemester() {
		return semester;
	}

	public void setSemester(String semester) {
		this.semester = semester;
	}

	

	public ArrayList<String> getSubject() {
		return subject;
	}

	public void setSubject(ArrayList<String> subject) {
		this.subject = subject;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public ArrayList<String> getEnrolledList() {
		return enrolledList;
	}

	public void setEnrolledList(ArrayList<String> enrolledList) {
		this.enrolledList = enrolledList;
	}

	public String execute() throws Exception {
		User user=new User();
		Map<String, Object> session = ActionContext.getContext().getSession();
		user = (User) session.get("user");
		
		roll_no=user.getRollNor();
		System.out.println("m here now");
		this.allSemValues=Lookup.find("semester"," where is_current='Y'");
		
		for (int i = 0; i < allSemValues.size(); i++){
       	Lookup list = allSemValues.get(i);
       	semesterList.add(list.getName());
       	MyLog.log("semester name:"+list.getName());
		}
		/*printing values for testing*/
		for(int i=0;i<semesterList.size();i++)
   		System.out.println("In home.java semester list is"+semesterList.get(i));
		/*Subject list*/
		int semesterid=0;
		System.out.println("bullshit semester is "+semester);
		semesterid = Semester.getSemesterId(semester);
		System.out.println("bull semester id is"+semesterid);
		if(semester != null){
			System.out.println("semester selected in java page is"+semester);
			
			this.allCourseValues=Lookup.find("course"," where semester_id="+semesterid+" and subject_id not in (select subject_id from enrollment where roll_no='"+roll_no+"')");
		
		}
		else{
			System.out.println(" no semester selected in java page is"+semester);
			this.allCourseValues=Lookup.find("course","");
		}
		for (int i = 0; i < allCourseValues.size(); i++){
        	Lookup list = allCourseValues.get(i);
        	subjectList.add(list.getSubject_name());
        	
        	System.out.println("semester id is now and then bullshitttt"+semesterid);
        	MyLog.log("course name:"+list.getName());
        }
		System.out.println("sruti is not a bull"+subjectList.size());
		enrolledList=(Enrollment.getSubjectListForSemester(roll_no, semesterid));
		if (this.commandButton.startsWith("Enroll")) {
			System.out.println("m in enroll!!!!!");
			if (!this.semester.isEmpty() && !this.subject.isEmpty()){
				for(int i=0;i<subject.size();i++){
					String sub = subject.get(i);
					System.out.println("subject is "+sub);
					System.out.println("semester is "+semester);
					int subId=Subject.getSubjectId(sub);
					int semId=Semester.getSemesterId(semester);
					//if the student has already enrolled for the subject, an error message should be displayed.
					//insert the subject and semester in enrollment table.
					int recNo=Enrollment.insertintoenrollment(roll_no, subId, semId);
					if(recNo != 1){
						msg="Unable to update data";
						return "failure";
					}
				}
			}
			else if(this.semester==null && this.subject==null){
				System.out.println("no values in fields");
				msg="Enter values in the fields!!!";
				return "failure";
			}
			
			if(semester !=null){
				msg="Subjects enrolled successfully for semester "+semester;
				return "success";
			}	
		}
		
		return "initial";
	
	}
}
